package com.example.podcaster;

import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.example.podcaster.database.DBApplication;
import com.example.podcaster.util.PodcastListAdapter;

public class SearchableActivity extends SherlockActivity
{
    private ListView listView;

    // private Boolean searchedforCast;
    private Cursor cursor;

    private String SearchViewType;

    private Bundle appData;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchable);
        listView = (ListView) findViewById(R.id.searchableActivityList);

        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setTitle("");
        actionBar.setIcon(R.drawable.logo);

        appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
        if (appData != null)
        {
            SearchViewType = appData.getString("SearchView");
        }

        handleIntent(getIntent());

    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        setIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent)
    {
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doSearch(query);
        }
    }

    public void doSearch(String query)
    {
        if (SearchViewType.equals("PodcastList") || SearchViewType.equals("FavoritesList") || SearchViewType.equals("EpisodeList"))
            cursor = DBApplication.searchStringFromTableSingleSearchParameter("podcast", "_id, url, title, is_Video", "title", query);
        else if (SearchViewType.equals("GenreList"))
        {
            String genre = appData.getString("GenreName");
            cursor = DBApplication.searchStringWithGenre(genre, query);
        }
        else if (SearchViewType.equals("ShowList"))
        {
            String listId = appData.getString("listId");
            cursor = DBApplication.searchStringWithListId(listId, query);
            DBApplication.printCursor(cursor);
        }
        else if (SearchViewType.equals("ShowGenreList"))
            cursor = DBApplication.searchStringFromTableSingleSearchParameter("genre", "name", "name", query);
        else
            cursor = DBApplication.searchStringFromTableSingleSearchParameter("podcast", "_id, url, title, is_Video", "title", query);

        // Cursor Cursor2 =
        // DBApplication.searchStringFromTableMultipleSearchParameter("podcast",
        // "_id, url, title, is_Video",new String[]{"title","author"},query);
        // DBApplication.printCursor(Cursor2);

        // DBApplication.printCursor(cursor);
        createListView();
    }

    public void createListView()
    {
        if (SearchViewType.equals("PodcastList") || SearchViewType.equals("GenreList") || SearchViewType.equals("FavoritesList") || SearchViewType.equals("ShowList"))
        {
            String[] values = new String[cursor.getCount()];
            String[] names = new String[cursor.getCount()];
            int[] ids = new int[cursor.getCount()];
            int i = 0;
            while (cursor.moveToPosition(i))
            {
                ids[i] = cursor.getInt(0);

                names[i] = cursor.getString(2);

                if (cursor.getInt(3) == 0)
                {
                    values[i] = "audio";
                }
                else
                {
                    values[i] = "video";
                }
                i++;
            }
            listView.setAdapter(new PodcastListAdapter(SearchableActivity.this, values, names, ids, getSharedPreferences("prefs", 0)));
            listView.setOnItemClickListener(new OnItemClickListener()
            {

                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
                {
                    cursor.moveToPosition(position);
                    int id = cursor.getInt(0);
                    int is_video = cursor.getInt(3);
                    Intent intent = new Intent(SearchableActivity.this, EpisodeListActivity.class);
                    intent.putExtra("id", id);
                    intent.putExtra("video", is_video);
                    SearchableActivity.this.startActivity(intent);
                }

            });

        }
        else if (SearchViewType.equals("ShowGenreList"))
        {
            final String[] genrenames = new String[cursor.getCount()];
            DBApplication.printCursor(cursor);
            int i = 0;
            cursor.moveToPosition(0);
            while (i < genrenames.length)
            {
                genrenames[i] = cursor.getString(0);
                cursor.moveToNext();
                i++;
            }
            listView.setAdapter(new ArrayAdapter<String>(SearchableActivity.this, android.R.layout.simple_spinner_dropdown_item, genrenames));
            listView.setOnItemClickListener(new OnItemClickListener()
            {

                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
                {
                    Intent intent = new Intent(SearchableActivity.this, GenreListActivity.class);
                    intent.putExtra("genreName", genrenames[position]);

                    startActivity(intent);
                }

            });
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Abfangen der ActionBar Items
        switch (item.getItemId())
        {
            case R.id.menu_home:
                Intent intent = new Intent(SearchableActivity.this, HomeActivity.class);
                SearchableActivity.this.startActivity(intent);
                return true;
            case R.id.menu_player:
                Intent intent1 = new Intent(SearchableActivity.this, PlayEpisodeActivity.class);
                intent1.putExtra("episodeId", EpisodeListActivity.getSharedID());
                SearchableActivity.this.startActivity(intent1);
                return true;    
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu)
    {
        
        if(PlayEpisodeActivity.getVisited()==true)
        {
            getSupportMenuInflater().inflate(R.menu.actionbar_search, menu);
        }
        else
        {
            getSupportMenuInflater().inflate(R.menu.actionbar_player, menu);
        }
        
        return super.onCreateOptionsMenu(menu);
    }

}
